<?php


namespace app\admin\controller\card;


use app\common\controller\Backend;

use fast\CcProxy;
use think\Exception;

class User extends Backend
{
    public function test()
    {
        $cc = new CcProxy("http://39.107.233.11:88", "admin", "899022");
        /*echo $cc->index();*/

        echo $cc->add("test9999", "123456", "2021-12-02", "02:07:39");
    }



    public function index($appId = 0) {
        if (empty($appId)) {
            $this->error("应用ID不能为空");
        }

        try {
            $row = model('app\admin\model\card\Apps')->where("id", $appId)->find();
        } catch (Exception $e) {
            $this->error($e->getMessage());
            return;
        }

        if (!$row) {
            $this->error("该应用不存在");
        }


        $this->view->assign("statusList", [
            1 => "正常账户",
            0 => "停用账户"
        ]);
        $this->assign("appId", $appId);
        $this->assignconfig("appId", $appId);

        if ($this->request->isAjax()) {
            $cc = new CcProxy($row['domain'], $row['account'], $row['password']);
            $data = $cc->index();

            // 过滤器
            $filter = json_decode($this->request->param('filter', ''));

            if (!empty($filter)) {

                $where = [
                    "userid" => isset($filter->userid) ? $filter->userid : null,
                    "usepassword" => isset($filter->usepassword) ? $filter->usepassword : null,
                    "enable" => isset($filter->enable) ? $filter->enable : null,
                    "disabledate" => isset($filter->disabledate) ? $filter->disabledate : null,
                    "autodisable" => isset($filter->autodisable) ? $filter->autodisable : null,
                    "createtime" =>isset($filter->createtime) ? $filter->createtime : null,
                    "is_expire" =>isset($filter->is_expire) ? $filter->is_expire : null,
                ];

                $data = array_filter($data, function ($val) use ($filter, $where){
                    $status = true;
                    foreach ($where as $key => $value) {
                        if (!$status) {
                            break;
                        }
                        if (!is_null($value)) {
                            $status = $value == $val[$key];
                        }
                    }
                    return $status;
                });

                $data = array_values($data);
            }

            $limit = $this->request->param("limit", 10);
            $offset = $this->request->param("offset", 1);
            $total = count($data);

            if ($total > $offset) {
                $data = array_slice($data, $offset, $limit);
            }

            return json([
                "total" => $total,
                'rows' => $data
            ]);
        }

        return $this->view->fetch();
    }


    public function edit($userid = null, $appId = null, $remove = null)
    {
        if (empty($appId)) {
            $this->error("应用ID不能为空");
        }

        try {
            $row = model('app\admin\model\card\Apps')->where("id", $appId)->find();
        } catch (Exception $e) {
            $this->error($e->getMessage());
            return;
        }

        if (!$row) {
            $this->error("该应用不存在");
        }


        if (empty($userid)) {
            $this->error("编辑用户ID不能为空");
        }

        $cc = new CcProxy($row['domain'], $row['account'], $row['password']);
        $data = $cc->index();
        $user = array_filter($data, function ($val) use ($userid) {
            return $userid == $val['userid'];
        });

        if (count($user) > 1) {
            $this->error("存在多个同名账户，无法编辑");
        }else if (count($user) < 1) {
            $this->error("该账户不存在");
        }else{
            $user = array_values($user);
        }

        if ($remove == 1) {
            $cc->remove($userid);
            $this->success("删除成功");
        }

        if ($this->request->isAjax()) {
            $params = $this->request->param("row/a");

            $time = explode( " ", $params['apply_date']);

            $cc->edit($userid, $params['username'], $params['password'], $params['enable'], $params['autodisable'], $params['usepassword'], $time[0], $time[1]);

            $this->success("编辑成功");
        }

        $this->view->assign("statusList", [
            1 => "正常账户",
            0 => "停用账户"
        ]);

        $this->view->assign("autoDisableList", [
            1 => "自动到期",
            0 => "禁止到期"
        ]);

        $this->view->assign("usePasswordList", [
            1 => "启用",
            0 => "免密码"
        ]);


        $this->view->assign("row", $user[0]);
        return $this->view->fetch();
    }


    public function del($appId = null, $remove = null) {
        $ids = $this->request->post('ids', null);
        $this->edit($ids, $appId, 1);
    }
}